﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data.OleDb;
using FootballProject.BL.BuisnessObjects;

namespace Common
{
    public static class CsvParser
    {
        /// <summary>
        /// Provides *.csv files parsing.
        /// </summary>
        /// <param name="filePath">Path to the *.csv file.</param>
        /// <param name="fileName">Name of file to parse.</param>        
        public static void Parse(string filePath, string fileName)
        {            
            //Used the OleDB for reading *.csv file 
            OleDbConnection oledbcn = new OleDbConnection(
                "Provider=Microsoft.Jet.OLEDB.4.0;" +
                "Extended Properties='text;HDR=Yes;FMT=Delimited';" +
                "Data Source=" + filePath + ";");
            
            oledbcn.Open();
            
            OleDbCommand oledbcmd = new OleDbCommand();
            
            //Get all data from file
            oledbcmd.CommandText = "SELECT * FROM [" + fileName + "]";
            oledbcmd.Connection = oledbcn;
            
            OleDbDataReader reader = oledbcmd.ExecuteReader();
            List<MatchStatistic> statLsit = new List<MatchStatistic>();
            //collection for saving Division objects
            Hashtable tempDivisions = new Hashtable();
            //collection for saving Referees objects
            Hashtable tempReferees = new Hashtable();
            //collection for saving Teams objects
            Hashtable tempTeams = new Hashtable();

            while (reader.Read()) 
            {                
                try 
                {
                    statLsit.Add(CreateMatch(reader, tempDivisions, tempTeams, tempReferees));
                }
                catch (ArgumentException)
                {
                    return;
                }
            }
            oledbcn.Close();                    
        }

        /// <summary>
        /// Reads a data from file and creates the MatchStatistc object, that represent a one row from file.
        /// </summary>
        /// <param name="reader">The instance of OleDbDataReader class for reading the data file.</param>
        /// <param name="tempDivisions">Collection for saving objects, that represents the divisions from file(unique).</param>
        /// <param name="tempTeams">Collection for saving objects, that represents the teams from file(unique).</param>
        /// <param name="tempReferees">Collection for saving objects, that represents the referees from file(unique).</param>
        /// <returns></returns>
        private static MatchStatistic CreateMatch(OleDbDataReader reader, Hashtable tempDivisions, Hashtable tempTeams, Hashtable tempReferees)
        {
            string tempDiv = reader.GetValue(0).ToString();                  
            if (!tempDivisions.ContainsKey(tempDiv))
            {
                tempDivisions.Add(tempDiv, new Division(Guid.NewGuid(), tempDiv));
            }
            
            DateTime date = DateTime.TryParse(reader.GetValue(1).ToString(), out date) ? date : new DateTime(1900, 01, 01);
            
            string tempTeam = reader.GetValue(2).ToString();
            if (!tempTeams.ContainsKey(tempTeam))
            {
                tempTeams.Add(tempTeam, new TeamInfo(Guid.NewGuid(),tempTeam));
            }

            TeamStatistics homeTeam = new TeamStatistics(Guid.NewGuid(), tempTeams[tempTeam] as TeamInfo);

            tempTeam = reader.GetValue(3).ToString();
            if (!tempTeams.ContainsKey(tempTeam))
            {
                tempTeams.Add(tempTeam, new TeamInfo(Guid.NewGuid(), tempTeam));
            }

            TeamStatistics awayTeam = new TeamStatistics(Guid.NewGuid(), tempTeams[tempTeam] as TeamInfo);
            
            MatchStatistic match = new MatchStatistic(tempDivisions[tempDiv] as Division, homeTeam, awayTeam, date);

            match.HomeTeam.FullTimeGoals = (int)reader.GetValue(4);
            match.AwayTeam.FullTimeGoals = (int)reader.GetValue(5);

            string FTResult = reader.GetValue(6).ToString();
            match.FullTimeResult = ResultDataConverter.Convert(FTResult);

            match.HomeTeam.HalfTimeGoals = (int)reader.GetValue(7);
            match.AwayTeam.HalfTimeGoals = (int)reader.GetValue(8);

            string HTResult = reader.GetValue(9).ToString();
            match.HalfTimeResult = ResultDataConverter.Convert(HTResult);

            string tempReferee = reader.GetValue(10).ToString();
            if (!tempReferees.ContainsKey(tempReferee))
            {
                tempReferees.Add(tempReferee, new Referee(Guid.NewGuid(), tempReferee));
            }
            match.Referee = tempReferees[tempReferee] as Referee;

            match.HomeTeam.Shoots = (int)reader.GetValue(11);
            match.AwayTeam.Shoots = (int)reader.GetValue(12);
            match.HomeTeam.TargetShoots = (int)reader.GetValue(13);
            match.AwayTeam.TargetShoots = (int)reader.GetValue(14);
            match.HomeTeam.Fouls = (int)reader.GetValue(15);
            match.AwayTeam.Fouls = (int)reader.GetValue(16);
            match.HomeTeam.Corners = (int)reader.GetValue(17);
            match.AwayTeam.Corners = (int)reader.GetValue(18);
            match.HomeTeam.YellowCards = (int)reader.GetValue(19);
            match.AwayTeam.YellowCards = (int)reader.GetValue(20);
            match.HomeTeam.RedCards = (int)reader.GetValue(21);
            match.AwayTeam.RedCards = (int)reader.GetValue(22);

            return match;
        }
    }
}
